<?php


class TbfClassLoader {
    static function Init(){

    }
    //添加一个不去掉class前缀的autoload, pathPrefix末尾不包含'/'
    static function addNotTrimPrefix($classPrefix,$pathPrefix){
        spl_autoload_register(function($class)use($classPrefix,$pathPrefix){
            $prefix = $classPrefix;
            $len = strlen($prefix);
            if (strncmp($prefix, $class, $len) !== 0) {
                return;
            }
            $file = $pathPrefix.'/'. str_replace('\\', '/',$class) . '.php';
            if (file_exists($file)) {
                require $file;
            }
        });
    }
    //添加一个去掉class前缀的autoload,classPrefix包含末尾的'\\' pathPrefix末尾不包含'/'
    static function addTrimPrefix($classPrefix,$pathPrefix){
        spl_autoload_register(function($class)use($classPrefix,$pathPrefix){
            $prefix = $classPrefix;
            $base_dir = $pathPrefix;
            $len = strlen($prefix);
            if (strncmp($prefix, $class, $len) !== 0) {
                return;
            }
            $relative_class = substr($class, $len);
            $file = $base_dir .'/'. str_replace('\\', '/', $relative_class) . '.php';
            if (file_exists($file)) {
                require $file;
            }
        });
    }
    static function addClassMap($expectClass,$filePath){
        spl_autoload_register(function($class)use($expectClass,$filePath){
            if ($class!==$expectClass){
                return;
            }
            if (file_exists($filePath)) {
                require $filePath;
            }
        });
    }
}